/**
 * 
 */
package com.wbcs.system.component.application.report.abstractreport;

import com.wbcs.config.component.IComponentConfigBean;
import com.wbcs.config.component.application.report.ColBean;
import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.config.xml.XmlElementBean;
import com.wbcs.exception.WbcsRuntimeException;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.assistant.EditableReportAssistant;
import com.wbcs.system.component.application.report.EditableListReportType2;
import com.wbcs.system.component.application.report.configbean.ColAndGroupDisplayBean;
import com.wbcs.system.component.application.report.configbean.ServerJsonServiceBean;
import com.wbcs.system.component.container.AbsContainerType;
import com.wbcs.system.function.FuncAssistant;
import com.wbcs.system.function.FuncContextBean;
import com.wbcs.system.function.IFunc;
import com.wbcs.system.function.IFuncDo;
import com.wbcs.system.tags.component.AbsComponentTag;
import com.wbcs.util.Consts;
import com.wbcs.util.Tools;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author hawkfly
 */
public abstract class AbsRequireChainReportType extends EditableListReportType2 implements IRequireChainParam, IFuncDo {
	
	private static Log log=LogFactory.getLog(AbsRequireChainReportType.class);
	
	protected IFunc requireFuncObj;
	protected ServerJsonServiceBean jsonBean;
	protected FuncContextBean context;
	
	public AbsRequireChainReportType(AbsContainerType parentContainerType,
			IComponentConfigBean comCfgBean, ReportRequest rrequest) {
		super(parentContainerType, comCfgBean, rrequest);
		// TODO Auto-generated constructor stub
		
	}

	@Override
	public void displayOnPage(AbsComponentTag displayTag)
	{
		//创建函数对象
		if(rrequest != null)
		{
			 FuncAssistant.getInstance().execFunc(this);
		}
	}
	
	@Override
	public int afterReportLoading(ReportBean reportbean,List<XmlElementBean> lstEleReportBeans)
	{
		 super.afterReportLoading(reportbean, lstEleReportBeans);
		 /*
		  * 读取requireParam、forwardParam参数，存入ReportBean
		  * 解析function表达式并创建相应的function object,存入必要的参数
		  */
		 XmlElementBean eleBean = lstEleReportBeans.get(0);
		 String requireParam = eleBean.attributeValue("requireParam");
		 
		 reportbean.setRequireParam(requireParam);
		 
		 //svrjsonsvc报表类型允许对属性requireParam不进行配置
		 if(Tools.isEmpty(requireParam, true))
		 {
			 return 1;
		 }
		 
		 if(!requireParam.startsWith(Consts.FUNC_PREFIX))
		 {
			log.error("requireParam参数只能接受函数定义");
			return 0;
		 }
		 
		 if(require(requireParam) == null)
		 {
			 return 0;
		 }
		 
		 return 1;
	}

	@Override
	public void doSelect() {
		jsonBean = new ServerJsonServiceBean();
		jsonBean.setLstReportData(lstReportData);
		jsonBean.setMetaData(showMetaData());
		List<ColBean> lsColBean = rbean.getDbean().getLstCols();
		List<Object> dataHeader = new ArrayList<Object>();
		for (final ColBean cBean : lsColBean) {
			dataHeader.add(new HashMap<String,Object>(){{
				put("column", cBean.getColumn());
				put("label", cBean.getLabel());
				put("align", cBean.getValuealign());
				put("width", cBean.getWidth());
			}});
		}
		jsonBean.setDataHeader(dataHeader);
		jsonBean.setDisplayRowInfo(cacheDataBean.getRealPageno())
				.setRecordCount(cacheDataBean.getRecordcount())
				.setPageSize(cacheDataBean.getPagesize());
	}
	/* (non-Javadoc)
	 * @see com.wbcs.system.component.application.report.abstractreport.IRequireChainParam#require(java.lang.String)
	 */
	@Override
	public IFunc require(String strFunc) {
		// TODO Auto-generated method stub
		
		requireFuncObj = FuncAssistant.getInstance().getHadParamsRequireFunc(strFunc);
		
		return requireFuncObj;
	}
	
	@Override
	public void doInsert() {
		// TODO Auto-generated method stub
		//改变 REPORTGUID_SAVEDATAS（前台传递存储数据标准） 的名字为 REPORTID_INSERTDATAS
		String saveDatasName = rbean.getGuid() + "_SAVEDATAS";
		String saveDatasVal = rrequest.getStringAttribute(rbean.getId(), saveDatasName, "");
		if(Tools.isEmpty(saveDatasVal, true))
		{
			throw new WbcsRuntimeException("未取到要保存的数据");
		}
		rrequest.removeAttribute(saveDatasName);
		String insertDatasName = rbean.getId() + "_INSERTDATAS";
		rrequest.setAttribute(insertDatasName, saveDatasVal);

		//重新调用EditableReportAssistant.getInstance().doAllReportsSaveAction(rrequest);
		EditableReportAssistant.getInstance().doAllReportsSaveAction(rrequest);
	}

	@Override
	public void doUpdate() {
		// TODO Auto-generated method stub
		//改变 REPORTGUID_SAVEDATAS （前台传递存储数据标准）的名字为 REPORTID_UPDATEDATAS
		String saveDatasName = rbean.getGuid() + "_SAVEDATAS";
		String saveDatasVal = rrequest.getStringAttribute(rbean.getId(), saveDatasName, "");
		if(Tools.isEmpty(saveDatasVal, true))
		{
			throw new WbcsRuntimeException("未取到要保存的数据");
		}
		rrequest.removeAttribute(saveDatasName);
		String insertDatasName = rbean.getId() + "_UPDATEDATAS";
		rrequest.setAttribute(insertDatasName, saveDatasVal);

		//重新调用EditableReportAssistant.getInstance().doAllReportsSaveAction(rrequest);
		EditableReportAssistant.getInstance().doAllReportsSaveAction(rrequest);
	}

	@Override
	public void error(String errorInfo){
		rrequest.getWResponse().getMessageCollector().error(errorInfo,true);
	}

	@Override
	public FuncContextBean getFuncContext(String upChainForwarParaName) {
		// TODO Auto-generated method stub
		if(context == null)
			context = new FuncContextBean(rbean, requireFuncObj);

		if(upChainForwarParaName != null)
			context.setForwardPara(upChainForwarParaName,rrequest.getAttribute(upChainForwarParaName));

		return context;
	}
}
